package com.agrimall.service.impl;

import com.agrimall.entity.Product;
import com.agrimall.mapper.ProductMapper;
import com.agrimall.service.ProductService;
import com.agrimall.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 商品服务实现类
 */
public class ProductServiceImpl implements ProductService {
    
    @Override
    public Product getProductById(Long productId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.selectById(productId);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public boolean addProduct(Product product) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            if (product.getStatus() == null) {
                product.setStatus(1);
            }
            int result = mapper.insert(product);
            sqlSession.commit();
            return result > 0;
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            return false;
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public boolean updateProduct(Product product) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int result = mapper.update(product);
            sqlSession.commit();
            return result > 0;
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            return false;
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public boolean deleteProduct(Long productId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int result = mapper.delete(productId);
            sqlSession.commit();
            return result > 0;
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            return false;
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> getProductList(int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectAll(offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> getProductListByMerchant(Long merchantId, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectByMerchantId(merchantId, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> getProductListByCategory(String category, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectByCategory(category, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> searchProducts(String keyword, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.searchByName(keyword, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countAllProducts() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countAll();
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countProductsByCategory(String category) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countByCategory(category);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countProductsByKeyword(String keyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countByKeyword(keyword);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countProductsByMerchant(Long merchantId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countByMerchantId(merchantId);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> getProductListByMerchantExcludeDeleted(Long merchantId, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectByMerchantIdExcludeDeleted(merchantId, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countProductsByMerchantExcludeDeleted(Long merchantId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countByMerchantIdExcludeDeleted(merchantId);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> searchProductsByMerchantAndName(Long merchantId, String keyword, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectByMerchantIdAndName(merchantId, keyword, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countProductsByMerchantAndName(Long merchantId, String keyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countByMerchantIdAndName(merchantId, keyword);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> getProductListByMerchantAndCategory(Long merchantId, String category, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectByMerchantIdAndCategory(merchantId, category, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countProductsByMerchantAndCategory(Long merchantId, String category) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countByMerchantIdAndCategory(merchantId, category);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> searchProductsByMerchantNameAndCategory(Long merchantId, String keyword, String category, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectByMerchantIdNameAndCategory(merchantId, keyword, category, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countProductsByMerchantNameAndCategory(Long merchantId, String keyword, String category) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countByMerchantIdNameAndCategory(merchantId, keyword, category);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> getProductListForAdmin(int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectAllForAdmin(offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countAllProductsForAdmin() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countAllForAdmin();
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public List<Product> searchProductsForAdmin(String keyword, String category, String merchantUsername, int page, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int offset = (page - 1) * pageSize;
            return mapper.selectForAdminByConditions(keyword, category, merchantUsername, offset, pageSize);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public int countProductsForAdminByConditions(String keyword, String category, String merchantUsername) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.countForAdminByConditions(keyword, category, merchantUsername);
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public boolean logicalDeleteProduct(Long productId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int result = mapper.logicalDelete(productId);
            sqlSession.commit();
            return result > 0;
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            return false;
        } finally {
            sqlSession.close();
        }
    }
    
    @Override
    public boolean restoreProduct(Long productId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            int result = mapper.restoreProduct(productId);
            sqlSession.commit();
            return result > 0;
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            return false;
        } finally {
            sqlSession.close();
        }
    }
}




